package com.etcsoft.pagedesigner.wizard;

import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.etcsoft.pagedesigner.common.Messages;
import com.etcsoft.pagedesigner.dialog.FolderOfProjectDialog;
import com.etcsoft.pagedesigner.dialog.ProjectListDialog;
import com.etcsoft.pagedesigner.model.ProjectItem;
import com.etcsoft.pagedesigner.model.ProjectManager;
import com.etcsoft.pagedesigner.util.CommonUtil;

/**
 * @author korman21@gmail.com
 */
public class NewFolderWizardFirstPage extends WizardPage {
    private Text txtProjectNm;
    private Text txtParentFolderNm;
    private Text txtFolderNm;

    private Button btnFindProject;
    private Button btnFindParentFolder;

    private ProjectItem parentProjectItem;

    public NewFolderWizardFirstPage() {
        super("");
        this.setTitle(Messages.DESC_FOLDER_WIZARD_FIRST_PAGE_TITLE);
        this.setDescription(Messages.DESC_FOLDER_WIZARD_FIRST_PAGE_DESC);
    }

    @Override
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);
        this.setControl(container);
        container.setLayout(new GridLayout(3, false));

        this.createProjectArea(container);
        this.createParentFolderArea(container);
        this.createFolderArea(container);
    }

    /**
     * @param container
     */
    private void createFolderArea(Composite container) {
        Label lblFolderNm = new Label(container, SWT.NONE);
        lblFolderNm.setText(Messages.DESC_FOLDER_NM);

        this.txtFolderNm = new Text(container, SWT.BORDER);
        this.txtFolderNm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
        this.txtFolderNm.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                String result = CommonUtil.chkNamingRule(NewFolderWizardFirstPage.this.txtFolderNm.getText(), null);
                NewFolderWizardFirstPage.this.setErrorMessage(result);
                NewFolderWizardFirstPage.this.updatePageComplete();
            }
        });
    }

    /**
     * @param container
     */
    private void createParentFolderArea(Composite container) {
        Label lblParentFolderNm = new Label(container, SWT.NONE);
        lblParentFolderNm.setText(Messages.DESC_PARENT_FOLDER);

        this.txtParentFolderNm = new Text(container, SWT.BORDER);
        this.txtParentFolderNm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        this.txtParentFolderNm.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                String result = CommonUtil.chkNamingRule(NewFolderWizardFirstPage.this.txtParentFolderNm.getText(), "[/]*");
                NewFolderWizardFirstPage.this.setErrorMessage(result);
                NewFolderWizardFirstPage.this.updatePageComplete();
            }
        });

        this.btnFindParentFolder = new Button(container, SWT.NONE);
        this.btnFindParentFolder.setEnabled(false);
        this.btnFindParentFolder.setText(Messages.DESC_FIND);
        this.btnFindParentFolder.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                FolderOfProjectDialog dialog = new FolderOfProjectDialog(NewFolderWizardFirstPage.this.getShell(), NewFolderWizardFirstPage.this.txtProjectNm.getText());
                if (dialog.open() == Window.OK) {
                    NewFolderWizardFirstPage.this.txtParentFolderNm.setText(dialog.getSelectParentFolder());
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
    }

    /**
     * @param container
     */
    private void createProjectArea(Composite container) {
        Label lblProjectNm = new Label(container, SWT.NONE);
        lblProjectNm.setText(Messages.DESC_PROJECT_NM);

        this.txtProjectNm = new Text(container, SWT.BORDER);
        this.txtProjectNm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        this.txtProjectNm.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                String result = CommonUtil.chkNamingRule(NewFolderWizardFirstPage.this.txtProjectNm.getText(), null);
                if (result == null) {
                    NewFolderWizardFirstPage.this.btnFindParentFolder.setEnabled(true);
                } else {
                    NewFolderWizardFirstPage.this.btnFindParentFolder.setEnabled(false);
                }
                NewFolderWizardFirstPage.this.setErrorMessage(result);
                NewFolderWizardFirstPage.this.updatePageComplete();
            }
        });

        this.btnFindProject = new Button(container, SWT.NONE);
        this.btnFindProject.setText(Messages.DESC_FIND);
        this.btnFindProject.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                ProjectListDialog dialog = new ProjectListDialog(NewFolderWizardFirstPage.this.getShell());
                if (dialog.open() == Window.OK) {
                    NewFolderWizardFirstPage.this.txtProjectNm.setText(dialog.getSelectProjectName());
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {

            }
        });
    }

    private void updatePageComplete() {
        boolean isCompleted = false;

        String projectNm = this.txtProjectNm.getText();
        String parentFolderNm = this.txtParentFolderNm.getText();
        String folderNm = this.txtFolderNm.getText();

        if (projectNm != null && projectNm.length() > 0) {
            ProjectItem parentProjectItem = ProjectManager.getManager().existProject(projectNm);
            if (parentProjectItem != null) {
                if (parentFolderNm != null && parentFolderNm.length() > 0) {
                    parentProjectItem = ProjectManager.getManager().existProjectItem(parentProjectItem, parentFolderNm);
                    if (parentProjectItem != null) {
                        if (folderNm != null && folderNm.length() > 0) {
                            if (ProjectManager.getManager().existProjectItem(parentProjectItem, folderNm) == null) {
                                isCompleted = true;
                                this.parentProjectItem = parentProjectItem;
                            } else {
                                NewFolderWizardFirstPage.this.setErrorMessage(NLS.bind(Messages.MSG_EXIST_FOLDER, folderNm));
                            }
                        }
                    } else {
                        NewFolderWizardFirstPage.this.setErrorMessage(NLS.bind(Messages.MSG_NOT_EXIST_PARENT_FOLDER, parentFolderNm));
                    }
                }
            } else {
                NewFolderWizardFirstPage.this.setErrorMessage(NLS.bind(Messages.MSG_NOT_EXIST_PROJECT, projectNm));
            }
        }

        this.setPageComplete(isCompleted);
    }

    public ProjectItem getParentProjectItem() {
        return this.parentProjectItem;
    }

    public String getFolderNm() {
        return this.txtFolderNm.getText();
    }
}
